package com.boesch.electroOffice.business.businessObjectImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boesch.electroOffice.business.businessObject.AppointmentBusinessObject;
import com.boesch.electroOffice.business.common.BusinessException;
import com.boesch.electroOffice.dataaccess.common.IllegalDataAccessException;
import com.boesch.electroOffice.dataaccess.dao.AppointmentDao;
import com.boesch.electroOffice.dataaccess.dao.IdentifierDao;
import com.boesch.electroOffice.dataaccess.dao.messerli.CustomerDao;
import com.boesch.electroOffice.model.electroOffice.Appointment;
import com.boesch.electroOffice.model.electroOffice.Identifier;
import com.boesch.electroOffice.model.messerli.Customer;
import com.boesch.electroOffice.model.wrapper.AppointmentWrapper;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

@Service
public class AppointmentBusinessObjectImpl implements AppointmentBusinessObject, Serializable {

	private static final long serialVersionUID = -5795382342337262010L;
	private static final Log log = LogFactoryUtil.getLog(AppointmentBusinessObjectImpl.class);

	@Autowired
	private AppointmentDao appointmentDao;
	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private IdentifierDao identifierDao;

	public List<Appointment> readAllAppointmentsFromDataaccess() throws IllegalDataAccessException, BusinessException {
		return appointmentDao.readAllAppointments();
	}

	public List<AppointmentWrapper> readAllAppointmentWrapperFromDataaccess() throws IllegalDataAccessException, BusinessException {
		List<Appointment> appointments = appointmentDao.readAllAppointments();
		List<AppointmentWrapper> appointmentWrapper = new ArrayList<AppointmentWrapper>();
		for (Appointment appointment : appointments) {
			try {
				appointmentWrapper.add(wrapAppointment(appointment));
			} catch (BusinessException ex) {
				// do nothing at this moment
			} catch (IllegalDataAccessException ex) {
				// do nothing at this moment
			}
		}
		return appointmentWrapper;
	}

	public Appointment readAppointmentById(Integer appointmentId) throws IllegalDataAccessException, BusinessException {
		return appointmentDao.readAppointmentById(appointmentId);
	}

	public AppointmentWrapper readAppointmentWrapperById(int appointmentId) throws IllegalDataAccessException, BusinessException {
		Appointment appointment = appointmentDao.readAppointmentById(appointmentId);
		return wrapAppointment(appointment);
	}

	public String saveAppointment(Appointment appointment, User currentUser) throws IllegalDataAccessException, BusinessException {
		return appointmentDao.saveAppointmentOnDatabase(appointment, currentUser);
	}

	public String updateAppointment(Appointment appointment, User currentUser) throws IllegalDataAccessException, BusinessException {
		return appointmentDao.updateAppointmentOnDatabase(appointment, currentUser);
	}

	public String openAppointment(Appointment appointment, User currentUser) throws IllegalDataAccessException, BusinessException {
		Identifier state = identifierDao.getIdentifierByEntityAndIdentifier("APPOINTMENT-STATE", "OPEN");
		appointment.setState(state);
		return appointmentDao.updateAppointmentOnDatabase(appointment, currentUser);
	}

	public String closeAppointment(Appointment appointment, User currentUser) throws IllegalDataAccessException, BusinessException {
		Identifier state = identifierDao.getIdentifierByEntityAndIdentifier("APPOINTMENT-STATE", "CLOSED");
		appointment.setState(state);
		return appointmentDao.updateAppointmentOnDatabase(appointment, currentUser);
	}

	private AppointmentWrapper wrapAppointment(Appointment appointment) throws IllegalDataAccessException, BusinessException {
		try {
			AppointmentWrapper aWrapper = new AppointmentWrapper();

			aWrapper.setAppointment(appointment);
			aWrapper.setCustomerAddress(readCustomer(appointment.getIdCustomerAddress()));
			aWrapper.setInvoiceAddress(readCustomer(appointment.getIdInvoiceAddress()));
			aWrapper.setObjectAddress(readCustomer(appointment.getIdObjectAddress()));
			aWrapper.setUser(UserLocalServiceUtil.getUser(appointment.getLrUserId()));

			return aWrapper;
		} catch (PortalException e) {
			log.error("Failed to read user from liferay for appointment: " + appointment.getId(), e);
			throw new BusinessException("");
		} catch (SystemException e) {
			log.error("Failed to read user from liferay for appointment: " + appointment.getId(), e);
			throw new BusinessException("");
		}
	}

	private Customer readCustomer(int customerId) throws IllegalDataAccessException {
		Customer customer = new Customer();
		if (customerId != 0) {
			customer = customerDao.readCustomerByCustomerId(customerId);
		}
		return customer;
	}
}
